package com.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.admin.mapper.SysDictDataMapper;
import com.cloud.admin.model.entity.SysDictData;
import com.cloud.admin.model.entity.SysUser;
import com.cloud.admin.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RuoYi首创 html调用 thymeleaf 实现字典读取
 *
 * @author douhaichao
 */
@Service("dict")
public class DictService {
    @Autowired
    private SysDictDataService dictDataService;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<SysDictData> getType(String dictType) {
//        return dictDataService.selectDictDataByType(dictType);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictType);
        return dictDataService.selectAll(sysDictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue) {
//        return dictDataService.selectDictLabel(dictType, dictValue);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictType);
        sysDictData.setDictValue(dictValue);
        SysDictData result = dictDataService.selectOne(sysDictData);
        if (result != null) {
            return result.getDictLabel();
        }
        return "";
    }

    /**
     * 根据用户ID查询平台
     *
     * @param user
     * @return 平台字典
     */
    public List<SysDictData> selectPlatform(SysUser user) {
        if (user.isAdmin()) {
            QueryWrapper<SysDictData> sysDictDataQueryWrapper = new QueryWrapper<>();
            sysDictDataQueryWrapper.eq("dict_type", "sys_platform");
            sysDictDataQueryWrapper.eq("status", 0);
            sysDictDataQueryWrapper.orderByDesc("is_default,dict_sort");
//            sysDictDataQueryWrapper.orderByAsc("dict_sort");
            return sysDictDataMapper.selectList(sysDictDataQueryWrapper);
        }
        return sysDictDataMapper.selectPlatform(user.getUserId());
    }
}
